#include "hk-28181-server.h"
#include "sip-server-cfg.h"
#include "sip-server.h"

bool hk_gb28181_sip_config_init(const char *pc_sip_id, const char *pc_sip_domain, const char *pc_sip_host, unsigned short us_sip_port, const char *pc_password)
{
    if((pc_sip_id == NULL) || (pc_sip_domain == NULL) || (pc_sip_host == NULL) || (pc_password == NULL) || (us_sip_port <= 1024)){
        return false;
    }

    std::string str_sip_id(pc_sip_id);
    std::string str_sip_domain(pc_sip_domain);
    std::string str_sip_host(pc_sip_host);
    std::string str_password(pc_password);

    return singleton_lock_sip_server_cfg_handler::Instance()->set_sip_server_cfg(str_sip_id, str_sip_domain, str_sip_host, us_sip_port, str_password);
}

bool hk_gb28181_set_media_server_cfg(const char *pc_media_ip, unsigned short us_port)
{
    if(pc_media_ip == NULL || us_port < 1024){
        return false;
    }

    std::string str_media_server_ip(pc_media_ip);
    return singleton_lock_sip_server_cfg_handler::Instance()->set_media_server_cfg(str_media_server_ip, us_port);
}

bool hk_gb28181_sip_server_start()
{
    return singleton_lock_sip_server_handler::Instance()->server_start_equipment();
}

bool hk_gb28181_set_register_callback(on_device_register_cb p_cb_fu, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_register_callback(p_cb_fu, p_user);
}

bool hk_gb28181_set_catalog_callback(on_device_catalog_cb p_cb_fun, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_catalog_callback(p_cb_fun, p_user);
}

bool hk_gb28181_set_invite_callback(on_device_channel_invite_cb p_cb_fun, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_invite_callback(p_cb_fun, p_user);
}

bool hk_gb28181_set_device_info_callback(on_device_info_cb p_cb_fun, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_device_info_callback(p_cb_fun, p_user);
}

bool hk_gb28181_set_device_status_callback(on_device_status_cb p_cb_fun, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_device_status_callback(p_cb_fun, p_user);
}

bool hk_gb28181_query_device_catalog(const char *pc_dev_id)
{
    if(pc_dev_id == NULL){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_request_catalog(str_dev_id);
}

bool hk_gb28181_query_device_deviceinfo(const char *pc_dev_id)
{
    if(pc_dev_id == NULL){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_request_deviceinfo(str_dev_id);
}

bool hk_gb28181_query_device_devicestatus(const char *pc_dev_id)
{
    if(pc_dev_id == NULL){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_request_devicestatus(str_dev_id);
}

bool hk_gb28181_query_device_recordinfo(const char *pc_dev_id, const char *pc_start_time, const char *pc_end_time)
{
    if(pc_dev_id == NULL || pc_start_time == NULL || pc_end_time == NULL){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_start_time(pc_start_time);
    std::string str_end_time(pc_end_time);
    return singleton_lock_sip_server_handler::Instance()->post_request_recordinfo(str_dev_id, str_start_time, str_end_time);
}

bool hk_gb28181_set_recordinfo_callback(on_device_recordinfo_cb p_cb_fun, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_recordinfo_callback(p_cb_fun, p_user);
}

bool hk_gb28181_control_device_ptz(const char *pc_dev_id, int i_ptz_cmd)
{
    if(pc_dev_id == NULL){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_control_ptz(str_dev_id, i_ptz_cmd);
}

bool hk_gb28181_control_device_ptz_set_preset(const char *pc_dev_id, int i_preset)
{
    if(pc_dev_id == NULL){
        return false;
    }

    //1 set
    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_control_ptz_preset(str_dev_id, 1, i_preset);
}

bool hk_gb28181_control_device_ptz_goto_preset(const char *pc_dev_id, int i_preset)
{
    if(pc_dev_id == NULL){
        return false;
    }

    //2 goto
    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_control_ptz_preset(str_dev_id, 2, i_preset);
}

bool hk_gb28181_control_device_ptz_del_preset(const char *pc_dev_id, int i_preset)
{
    if(pc_dev_id == NULL){
        return false;
    }

    //3 del
    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_control_ptz_preset(str_dev_id, 3, i_preset);
}

bool hk_gb28181_control_device_get_presetlist(const char *pc_dev_id)
{
    if(pc_dev_id == NULL){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    return singleton_lock_sip_server_handler::Instance()->post_request_presetlist(str_dev_id);
}

bool hk_gb28181_set_presetlist_callback(on_device_presetlist_cb p_cb_fun, void *p_user)
{
    return singleton_lock_sip_server_handler::Instance()->set_presetlists_callback(p_cb_fun, p_user);
}

bool hk_gb28181_realtime_play(const char *pc_dev_id, const char *pc_channel_id)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    return singleton_lock_sip_server_handler::Instance()->post_invite_play(str_dev_id, str_channel_id);
}

bool hk_gb28181_realtime_bye(const char *pc_dev_id, const char *pc_channel_id, const char *pc_call_id)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_call_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_call_id(pc_call_id);
    return singleton_lock_sip_server_handler::Instance()->post_invite_bye(str_dev_id, str_channel_id, str_call_id);
}

bool hk_gb28181_playback_play(const char *pc_dev_id, const char *pc_channel_id, const char *pc_start_time, const char *pc_end_time)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_start_time == NULL) || (pc_end_time == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_start_time(pc_start_time);
    std::string str_end_time(pc_end_time);

    return singleton_lock_sip_server_handler::Instance()->post_invite_playback(str_dev_id, str_channel_id, str_start_time, str_end_time);
}

bool hk_gb28181_playback_pause(const char *pc_dev_id, const char *pc_channel_id, const char *pc_call_id)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_call_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_call_id(pc_call_id);

    return singleton_lock_sip_server_handler::Instance()->post_invite_playback_pause(str_dev_id, str_channel_id, str_call_id);
}

bool hk_gb28181_playback_continue(const char *pc_dev_id, const char *pc_channel_id, const char *pc_call_id)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_call_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_call_id(pc_call_id);

    return singleton_lock_sip_server_handler::Instance()->post_invite_playback_continue(str_dev_id, str_channel_id, str_call_id);
}

bool hk_gb28181_playback_scale(const char *pc_dev_id, const char *pc_channel_id, const char *pc_call_id, float f_scale)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_call_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_call_id(pc_call_id);

    return singleton_lock_sip_server_handler::Instance()->post_invite_playback_scale(str_dev_id, str_channel_id, str_call_id, f_scale);
}

bool hk_gb28181_playback_teardown(const char *pc_dev_id, const char *pc_channel_id, const char *pc_call_id)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_call_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_call_id(pc_call_id);

    return singleton_lock_sip_server_handler::Instance()->post_invite_playback_teardown(str_dev_id, str_channel_id, str_call_id);
}

bool hk_gb28181_playback_bye(const char *pc_dev_id, const char *pc_channel_id, const char *pc_call_id)
{
    if((pc_channel_id == NULL) || (pc_channel_id == NULL) || (pc_call_id == NULL)){
        return false;
    }

    std::string str_dev_id(pc_dev_id);
    std::string str_channel_id(pc_channel_id);
    std::string str_call_id(pc_call_id);

    return singleton_lock_sip_server_handler::Instance()->post_invite_playback_bye(str_dev_id, str_channel_id, str_call_id);
}
